package com.sam;



import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class LoginActivity extends Activity {

	Button btn, btn2;
	EditText username;
	EditText password;
	SocketService mBoundService; 
	boolean mIsBound = false;
	LoginActivity login=null;
	String[] values=null;
	TextView tv;
	protected static LoginActivity la;
	private ServiceConnection mConnection = new ServiceConnection() {
		// EDITED PART
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			mBoundService = ((SocketService.LocalBinder) service).getService();
			btn.setEnabled(true);
			
			if(values[0].equals("main")){
				if(!mBoundService.getSessionId().equals("none")){
				Intent i = new Intent();
				i.setClass(la, MainActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //
				String[] session = new String [1];
				
				i.putExtra("movies",mBoundService.getSessionId());
				startActivity(i);
				
				for(int j=0;j<2000;j++);
						finish();
				}
			}
			btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

					String[] loginData = new String[2]; 
					String message = "login";
					loginData[0] = username.getText().toString();
					loginData[1] = password.getText().toString();
					DataStream data = new DataStream(message,null,loginData); 
					mBoundService.getTCPClient().sendMessage(data);
				}
			});
			
			if(!mBoundService.getSessionId().equals("none")){
				
			
				
				Intent i = new Intent();
				i.setClass(la, MainActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //
				String[] session = new String [1];
				session[0]=mBoundService.getSessionId();
				i.putExtra("movies",session);
				startActivity(i);
				LoginActivity.la.finish();
			}
			
				

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			mBoundService = null;
		}

	};

	private void doBindService() {
		bindService(new Intent(this, SocketService.class), mConnection,
				Context.BIND_AUTO_CREATE);
		mIsBound = true;
		if (mBoundService != null) {
			mBoundService.IsBoundable();
		}
	}

	private void doUnbindService() {
		if (mIsBound) {
			// Detach our existing connection.
			unbindService(mConnection);
			mIsBound = false;
		}
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		login = this; 
		Bundle extras = getIntent().getExtras();
		values = new String[extras.getStringArray("parameters").length];
		values = extras.getStringArray("parameters");
		la=this;
		btn = (Button) findViewById(R.id.login);
		btn.setEnabled(false);
		tv = (TextView) findViewById(R.id.textView1);
		username = (EditText) findViewById(R.id.editText1);
		password = (EditText) findViewById(R.id.editText2);
		doBindService();
		
		
		

	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		doUnbindService();
	}
}